package com.yihuchess.simplechess;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;

import com.yihuchess.bluetoothConnect.BtInterfaceMananger;

/*  add for bluetooth chessboard api. */


public class MainActivity extends Activity
                implements OnClickListener, OnTouchListener {

    private static final String TAG = "MainActivity";

    Button mConnectBtBoard;
    BtInterfaceMananger mBtInterfaceMananger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= 23) {
            // Android M Permission check
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                if (!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                    String str = getResources().getString(R.string.permission_bluetooth);
                    String msg = getResources().getString(R.string.permission_denied, str);
                    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                } else {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                            mBtInterfaceMananger.PERMISSION_REQUEST_COARSE_LOCATION);
                }
            }

        }
        setContentView(R.layout.activity_main);
        initButton(R.id.btn_local_play);
        initButton(R.id.btn_network_play);

        initButton(R.id.btn_smartboard_setting);
        initButton(R.id.btn_connect_bt);
        mConnectBtBoard = (Button)findViewById(R.id.btn_connect_bt);
        mBtInterfaceMananger = BtInterfaceMananger.init(this, mConnectBtBoard);
    }

    private View initButton(int id) {
        View v = findViewById(id);
        if(v != null) {
            v.setOnClickListener(this);
            v.setOnTouchListener(this);
        }
        return v;
    }
    
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        if (v.getId() == R.id.btn_local_play) {
            mBtInterfaceMananger.onButtonInit();

            intent.setAction(Intent.ACTION_VIEW);
            intent.setClass(this, GameActivity.class);
            startActivity(intent);
            return;
        }
        if (v.getId() == R.id.btn_connect_bt) {
            startActivityForResult(mBtInterfaceMananger.onBtClick(), mBtInterfaceMananger.BT_BOARD_CONNECT_CODE);
            return;
        }
        if (v.getId() == R.id.btn_smartboard_setting) {
            mBtInterfaceMananger.creatDialog();
            return;
        }

        return;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_SCROLL:
            if (v.getId() == R.id.btn_connect_bt) {
                v.setBackgroundResource(R.drawable.bt_button_press);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (v.getId() == R.id.btn_connect_bt) {
                if (mBtInterfaceMananger.isBluetoothConnected()) {
                    v.setBackgroundResource(R.drawable.bt_button_connected);
                } else {
                    v.setBackgroundResource(R.drawable.bt_button);
                }
            }
            break;
        }
        return false;
    }

    @Override
	public void onResume() {
        mBtInterfaceMananger.onBtResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
        mBtInterfaceMananger.onBtDestory();
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
        case BtInterfaceMananger.BT_BOARD_CONNECT_CODE:
			if (resultCode == RESULT_OK) {				
				String returnName = data.getStringExtra(BtInterfaceMananger.BtBoardName);
                if (returnName != null) {
			        Log.e(TAG, "get name:" + returnName);
                }
            } 
            mBtInterfaceMananger.onConnectActivityReturn(true);
            break;
		default:
            mBtInterfaceMananger.onConnectActivityReturn(false);
			break;
		}
	}

}


